package com.hbun.cxcy.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbun.cxcy.entity.Apply;
import com.hbun.cxcy.service.ApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
@CrossOrigin
@Slf4j
@Controller
@RequestMapping("/cxcy/excel")
public class ExcelController {
    @Autowired
    ApplyService applyService;

    @RequestMapping("/down")
    public void downExcel(HttpServletResponse response) throws IOException {
        //学生数据

        //状态为1的进行下载，为0的表示删除
        List<Apply> data = applyService.list(new QueryWrapper<Apply>().eq("status", 1));
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生申请表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Apply.class)
                .sheet("学生申请表")
                .doWrite(data);
//        System.out.println("----------------Excel下载成功--------------");
        log.info("----------------Excel下载成功--------------");
    }
}
